#include "sqlink.c"
#include "Stackle_For_Char.c"
#include "Stackle_For_Reducer.c"

void Translator(StacklePtr Stack,char* expr)
{
    int i = 0;
    for(;expr[i] != '\0';i++)
    {
        Push(Stack,expr[i]);
    }
}

void Permutation_11(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,cur->exp[0],"0","0");
}

void Permutation_22(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,"0",cur->exp[1],"0");
}

void Permutation_33(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,"0","0",cur->exp[2]);
}

void Permutation_12(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,cur->exp[1],cur->exp[0],"0");
}

void Permutation_13(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,cur->exp[2],"0",cur->exp[0]);
}

void Permutation_23(Node* head,int order)
{
    int length = Get_Length(head);
    Node* cur = head->next;
    for(;cur->order != order;cur = cur->next);
    insertNode(head,cur->coefficience,"0",cur->exp[2],cur->exp[1]);
}

void Reducer(Node* head)
{
    Node* Eyes = head->next;
    Node* Pen_Point = Eyes->next;
    int length = Get_Length(head);
    int order = 1;
    Stackle_For_Reducer_Ptr Stack_For_Reducer = Stackle_For_Reducer_Init();
    for(;Eyes->next != NULL;Eyes = Eyes->next)
    {
        for(Pen_Point = Eyes->next;Pen_Point->next != NULL;Pen_Point = Pen_Point->next) //NULL Harzard
        {
            if((Eyes->exp[0] == Pen_Point->exp[0])&&(Eyes->exp[1] == Pen_Point->exp[1])&&(Eyes->exp[2] == Pen_Point->exp[2]))
            {
                Stackle_For_Reducer_Push(Stack_For_Reducer,Pen_Point);
            }
        }
        for(;Stack_For_Reducer->top != NULL;Stackle_For_Reducer_Pop(Stack_For_Reducer))
        {
            Eyes->coefficience += Stack_For_Reducer->top->term->coefficience;
            deleteNode(head,Stack_For_Reducer->top->term->order);
        }
    }
}

void Caculator(StacklePtr Stack,Node* head)
{
    Char_Link_Ptr Pen_Point = Stack->base;
    Node* term = head->next;
    int order = 1;
    int length = Get_Length(head);
    for(;order <= length;order++)
    {
        for(;Pen_Point->next != Stack->top;Pen_Point = Pen_Point->next->next)
        {
            if(Pen_Point->text == "1"&&Pen_Point->next->text == "1")
            {
                Permutation_11(head,order);
            }
            if(Pen_Point->text == "2"&&Pen_Point->next->text == "2")
            {
                Permutation_22(head,order);
            }
            if(Pen_Point->text == "3"&&Pen_Point->next->text == "3")
            {
                Permutation_33(head,order);
            }
            if(Pen_Point->text == "1"&&Pen_Point->next->text == "2")
            {
                Permutation_12(head,order);
            }
            if(Pen_Point->text == "1"&&Pen_Point->next->text == "3")
            {
                Permutation_13(head,order);
            }
            if(Pen_Point->text == "2"&&Pen_Point->next->text == "3")
            {
                Permutation_23(head,order);
            }
        }
        Pen_Point = Stack->base;
        term = term->next;
    }
}



void main()
{
    char expr[] = "12+13";
    Stackle* Stack = StackleInit();
    Node* head = initList();
    insertNode(head,1,"a","b","c");
    Translator(Stack,expr);
    Caculator(Stack,head);
    Reducer(head);
    traversal(head);
}